
###set working directory in the next line
setwd("") 


setwd("C:/Users/ylupu/Dropbox/IGO Communities")

all<-read.csv("dyadic_for_clubsofclubs_figure.csv", header = TRUE)

years<-unique(all$year)

#######europe

europeintra<-NA
for (i in 1:length(years)){
year<-years[i]
sub1<-all[which(all$samecomm==1 & all$year==year & all$commname1=="Europe"),]
sub2<-all[which(all$samecomm==1 & all$year==year & all$commname2=="Europe"),]
sub<-rbind(sub1,sub2)
europeintra[i]<-mean(sub$igos)
}

europeinter<-NA

for (i in 1:length(years)){
year<-years[i]
sub1<-all[which(all$samecomm==0 & all$year==year & all$commname1=="Europe"),]
sub2<-all[which(all$samecomm==0 & all$year==year & all$commname2=="Europe"),]
sub<-rbind(sub1,sub2)
europeinter[i]<-mean(sub$igos)
}
eurodiff<-(europeintra-europeinter)


#######americas

americaintra<-NA
for (i in 1:length(years)){
year<-years[i]
sub1<-all[which(all$samecomm==1 & all$year==year & all$commname1=="Latin America"),]
sub2<-all[which(all$samecomm==1 & all$year==year & all$commname2=="Latin America"),]
sub<-rbind(sub1,sub2)
americaintra[i]<-mean(sub$igos)
}

americainter<-NA

for (i in 1:length(years)){
year<-years[i]
sub1<-all[which(all$samecomm==0 & all$year==year & all$commname1=="Latin America"),]
sub2<-all[which(all$samecomm==0 & all$year==year & all$commname2=="Latin America"),]
sub<-rbind(sub1,sub2)
americainter[i]<-mean(sub$igos)
}

americadiff<-(americaintra-americainter)


#######africa

africaintra<-NA
for (i in 1:length(years)){
year<-years[i]
sub1<-all[which(all$samecomm==1 & all$year==year & all$commname1=="Africa"),]
sub2<-all[which(all$samecomm==1 & all$year==year & all$commname2=="Africa"),]
sub<-rbind(sub1,sub2)
africaintra[i]<-mean(sub$igos)
}

africainter<-NA

for (i in 1:length(years)){
year<-years[i]
sub1<-all[which(all$samecomm==0 & all$year==year & all$commname1=="Africa"),]
sub2<-all[which(all$samecomm==0 & all$year==year & all$commname2=="Africa"),]
sub<-rbind(sub1,sub2)
africainter[i]<-mean(sub$igos)
}

africadiff<-(africaintra-africainter)

##########plots

pdf("Figure 8.pdf", width = 9, height =8)

par(mfrow=c(3,2))

plot(years,europeintra,type="n",main="",bty="n",
       xlab="",
       ylab="",
       ylim=c(0,50),xlim=c(1950,2005))
lines(years,europeintra,lwd=2, col="black")
lines(years,europeinter,lwd=2, col="black",lty=3)
mtext("European/Northern Cluster",side=3,line=1.5,cex=1.1)
text(x=1952,y=48,"Mean Ties with Members of Same Cluster",pos=4)
text(x=1952,y=44,"Mean Ties with Members of Others Clusters",pos=4)
lines(x=c(1949.5,1952),y=c(48,48),lwd=2)
lines(x=c(1949.5,1952),y=c(44,44),lwd=2,lty=3)
mtext("Year",side=1,line=2.5,cex=1.1)
mtext("Mean IGO Ties",side=2,line=2.5,cex=1.1)

plot(years,eurodiff,type="n",main="",bty="n",
       xlab="",
       ylab="",
       ylim=c(0,25),xlim=c(1950,2005))
lines(years,eurodiff,lwd=2, col="black")
mtext("Year",side=1,line=2.5,cex=1.1)
mtext("European/Northern Cluster",side=3,line=1.5,cex=1.1)

text(x=1952,y=23,"Mean Difference Between Intra and Inter-Cluster Tie Rates",pos=4)
lines(x=c(1949.5,1952),y=c(23,23),lwd=2)


plot(years,americaintra,type="n",main="",bty="n",
       xlab="",
       ylab="",
       ylim=c(0,50),xlim=c(1950,2005))
lines(years,americaintra,lwd=2, col="black")
lines(years,americainter,lwd=2, col="black",lty=3)
mtext("Latin-American Cluster",side=3,line=1.5,cex=1.1)
text(x=1952,y=48,"Mean Ties with Members of Same Cluster",pos=4)
text(x=1952,y=44,"Mean Ties with Members of Others Clusters",pos=4)
lines(x=c(1949.5,1952),y=c(48,48),lwd=2)
lines(x=c(1949.5,1952),y=c(44,44),lwd=2,lty=3)
mtext("Year",side=1,line=2.5,cex=1.1)
mtext("Mean IGO Ties",side=2,line=2.5,cex=1.1)

plot(years,americadiff,type="n",main="",bty="n",
       xlab="",
       ylab="",
       ylim=c(0,25),xlim=c(1950,2005))
lines(years,americadiff,lwd=2, col="black")
mtext("Year",side=1,line=2.5,cex=1.1)
mtext("Latin-American Cluster",side=3,line=1.5,cex=1.1)

text(x=1952,y=23,"Mean Difference Between Intra and Inter-Cluster Tie Rates",pos=4)
lines(x=c(1949.5,1952),y=c(23,23),lwd=2)

plot(years,africaintra,type="n",main="",bty="n",
       xlab="",
       ylab="",
       ylim=c(0,50),xlim=c(1950,2005))
lines(years,africaintra,lwd=2, col="black")
lines(years,africainter,lwd=2, col="black",lty=3)
mtext("African Cluster",side=3,line=1.5,cex=1.1)
text(x=1952,y=48,"Mean Ties with Members of Same Cluster",pos=4)
text(x=1952,y=44,"Mean Ties with Members of Others Clusters",pos=4)
lines(x=c(1949.5,1952),y=c(48,48),lwd=2)
lines(x=c(1949.5,1952),y=c(44,44),lwd=2,lty=3)
mtext("Year",side=1,line=2.5,cex=1.1)
mtext("Mean IGO Ties",side=2,line=2.5,cex=1.1)

plot(years,africadiff,type="n",main="",bty="n",
       xlab="",
       ylab="",
       ylim=c(-5,25),xlim=c(1950,2005))
lines(years,africadiff,lwd=2, col="black")
mtext("Year",side=1,line=2.5,cex=1.1)
mtext("African Cluster",side=3,line=1.5,cex=1.1)

text(x=1952,y=23,"Mean Difference Between Intra and Inter-Cluster Tie Rates",pos=4)
lines(x=c(1949.5,1952),y=c(23,23),lwd=2)

dev.off()

